package cn.itcast.nems.order.service.impl;

import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.nems.base.util.DataBeanUtil;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import cn.itcast.nems.order.dao.entity.OrderExtend;
import cn.itcast.nems.order.dao.mapper.OrderExtendMapper;
import cn.itcast.nems.order.enumeration.OrderErrorBodyEnum;
import cn.itcast.nems.order.service.OrderExtendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 销售订单头-扩展属性 服务实现类
 * </p>
 *
 * @author liyong
 * @since 2023-08-31
 */
@Service
@Slf4j
@AllArgsConstructor
public class OrderExtendServiceImpl extends ServiceImpl<OrderExtendMapper, OrderExtend> implements OrderExtendService  {
    
    private final OrderExtendMapper orderExtendMapper ;

    @Override
    public void insertByProperties(String orderId,String operatorEmail, Object bean, String... properties) {
        try {
            Map<String, Object> map = CustomBeanUtil.objectAttributeMap(bean, properties);
            map.forEach((k, v) -> {
                if (Objects.nonNull(v)) {
                    OrderExtend orderExtend = new OrderExtend();
                    orderExtend.setOrderId(orderId);
                    orderExtend.setName(k);
                    orderExtend.setValue(String.valueOf(v));
                    DataBeanUtil.create(orderExtend, operatorEmail);
                    orderExtendMapper.insert(orderExtend);
                }
            });
        } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
            log.info("[报名页面]新增订单头拓展信息失败,  异常: {}", e.getMessage());
            // throw new BizException(CustomBizError.message(BizError.BIZ_DATA_EXCEPTION, "参数异常"));
            BizExceptionProducer.throwProduce(OrderErrorBodyEnum.ORDER_EXTEND_INSERT_ERROR);
        }
    }

}
